package com.aaa.vip.service.impl;

import com.aaa.common.entity.UserBalanceLog;
import com.aaa.common.util.TokenUtil;
import com.aaa.vip.dao.UserBalanceLogDao;
import com.aaa.vip.service.UserBalanceLogService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 余额详细(UserBalanceLog)表服务实现类
 *
 * @author makejava
 * @since 2025-09-25 19:09:13
 */
@Service("userBalanceLogService")
public class UserBalanceLogServiceImpl implements UserBalanceLogService {
    @Resource
    private UserBalanceLogDao userBalanceLogDao;

    @Resource
    private HttpServletRequest request;

    /**
     * 查询余额selectByUserIdTopUp详细token
     *
     * @return
     */
    @Override
    public List<UserBalanceLog> selectByUserIdTopUp() {
        String userId = TokenUtil.getUserId(request);
        return this.userBalanceLogDao.selectByUserIdTopUp(Integer.valueOf(userId));
    }

    /**
     * 查询余额selectByUserIdPayment详细token
     *
     * @return
     */
    @Override
    public List<UserBalanceLog> selectByUserIdPayment() {
        String userId = TokenUtil.getUserId(request);
        return this.userBalanceLogDao.selectByUserIdPayment(Integer.valueOf(userId));
    }

    /**
     * 查询余额详细token
     *
     * @return
     */
    @Override
    public List<UserBalanceLog> selectByUserId() {
        String userId = TokenUtil.getUserId(request);
        return this.userBalanceLogDao.selectByUserId(Integer.valueOf(userId));
    }

    /**
     * 通过ID查询单条数据
     *
     * @param balanceLogId 主键
     * @return 实例对象
     */
    @Override
    public UserBalanceLog queryById(Integer balanceLogId) {
        return this.userBalanceLogDao.queryById(balanceLogId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<UserBalanceLog> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        UserBalanceLog userBalanceLog = page.getData();
        return new PageInfo(userBalanceLogDao.queryAllByLimit(userBalanceLog));
    }

    /**
     * 新增数据
     *
     * @param userBalanceLog 实例对象
     * @return 实例对象
     */
    @Override
    public UserBalanceLog insert(UserBalanceLog userBalanceLog) {
        this.userBalanceLogDao.insert(userBalanceLog);
        return userBalanceLog;
    }

    /**
     * 修改数据
     *
     * @param userBalanceLog 实例对象
     * @return 实例对象
     */
    @Override
    public UserBalanceLog update(UserBalanceLog userBalanceLog) {
        this.userBalanceLogDao.update(userBalanceLog);
        return this.queryById(userBalanceLog.getBalanceLogId());
    }

    /**
     * 通过主键删除数据
     *
     * @param balanceLogId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer balanceLogId) {
        return this.userBalanceLogDao.deleteById(balanceLogId) > 0;
    }
}
